<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>查看用户优惠券信息</title>
    <script src="/layui/layui.js"></script>
    <link rel="stylesheet" href="/layui/css/layui.css"/>
    <script src="/jquery/jquery-3.2.1.min.js"></script>
    <link rel="stylesheet" href="/css/Coupons/giveUserCouponList.css">
</head>
<body>
<div class="content layui-row">
    <div class="layui-col-xs11 layui-col-sm11 layui-col-md11">
        <form class="layui-row layui-form">
            <div class="layui-form-item">
                <div class="layui-input-inline ">
                    <input type="text" name="couponName" placeholder="优惠券名称" class="layui-input">
                </div>
                <button type="button" onclick="search()" class="layui-icon layui-icon-search layui-btn layui-btn-primary" style="margin-left: -10px"></button>
            </div>
        </form>
    </div>
    <div class="layui-col-xs11 layui-col-sm11 layui-col-md11">
        <table id="demo" lay-filter="test"></table>
    </div>
    <div class="layui-col-xs11 layui-col-sm11 layui-col-md11" style="text-align: right;">
        <button type="button" class="layui-btn" onclick="resert()">取消</button>
        <button type="button" class="layui-btn" onclick="confirm(true)">确定</button>
    </div>
</div>
</body>
<script>
    var $,
        table;

    layui.use(['jquery', 'table'], function () {
        $ = layui.jquery,
            table = layui.table;

        var user = JSON.parse(localStorage.getItem('user'));
        console.log(user);
        //查询所有优惠券
        $.ajax({
            url:'/Coupon/getTable',
            methos:'post',
            dataType:'json',
            success:function (data) {
                tab(data);
            }
        })

        function tab(data) {
            table.render({
                elem:'#demo',
                data:data.data,
                page:true,
                cols:[[
                    {type:'checkbox'},
                    {field:'couponName',title:'优惠券名称',align:'center'},
                    {field:'couponPrice',title:'面额',align:'center'},
                    {field:'type',title:'使用条件',align:'center',templet:function (d) {
                            if(d.type == 2){
                                return "商品";
                            }else if(d.type == 3){
                                return "视频";
                            }else if(d.type == null) {
                                return "";
                            }
                        }},
                    {title:'有效期',align:'center', templet:function(data){
                            return "<p>开始：" + data.startDate + "</p>" + '\n'
                                +"<p>结束：" + data.endDate + "</p>";
                        }},
                ]],
                limit:5
            })
        }

        //根据优惠券名称查询
        search = function search() {
            var couponName = $("input[name = couponName]").val();//优惠券名称
            $.ajax({
                url:'/Coupon/queryCouponByName',
                data:{'couponName':couponName},
                dataType:'json',
                methos:'post',
                success:function (data) {
                    tab(data);
                }
            })
        }

        //查询用户信息
        confirm(false);
        //赠送优惠券
        function confirm(flag){
            var checkStatus = table.checkStatus('demo');
            if(flag){
                if (checkStatus.data.length > 0){
                    checkStatus.data.forEach(function (data){
                        $.ajax({
                            url: '/CouponUsed/addCouponUsed',
                            type: 'post',
                            dataType: 'json',
                            data: {
                                'couponId': data.couponId,
                                'userId': id,
                                'used': data.used + 1,
                                'updateId': user.userId,
                            },
                            success: function (item) {
                                if(item.data){
                                    var index = parent.layer.index;
                                    parent.layer.close(index);
                                    tab();
                                }
                            }
                        });
                    });
                }
            }
        }

        //取消
        resert = function resert() {
            var index = parent.layer.index;
            parent.layer.close(index);
        }
    })
</script>
</html>